<?php

class IndexController extends Frontend_Base_Controller
{

    public function init()
    {
        parent::init();
    }

    public function indexAction()
    {
        $this->pageAction('index');
        $this->_helper->viewRenderer('page'); 
        return;
    }

    /**
     * contactAction
     *
     * This action provides contact form functionality.
     */
    public function contactAction()
    {
        $request = $this->getRequest();
        $form    = new Application_Form_Contact();
 
        if ($this->getRequest()->isPost())
        {
            if ($form->isValid($request->getPost()))
            {

                $general_settings = Zend_Registry::get('general');
                $form_values      = $form->getValues();

                $mail_sender = new Application_Model_Mail_Sender();
                $mail_sender->selectTemplate('contact_form_submitted');
                if ($mail_sender->hasTemplate())
                {
                    $mail_sender->setTo($general_settings['email']);
                    $mail_sender->setTokens(array(
                        'email'    => $form_values['email'],
                        'comment'  => $form_values['comment'],
                        'app_name' => $general_settings['name'],
                    ));
                    $mail_sender->send();
                }

                $this->_helper->flashMessenger->addMessage(array('success' => $this->view->translate('Your message was successfully sent!')));
                return $this->_helper->redirector('contact');
            }
        }
 
        $this->view->form = $form;
    }

    /**
     * pageAction
     *
     * This action is used to render database static pages content.
     */
    public function pageAction($key = false)
    {

        if (empty($key))
        {
            $request = $this->getRequest();
            $key     = $request->getParam('key');
        }

        if (!empty($key) && is_scalar($key))
        {

            $mapper = new Application_Model_PageMapper();
            $model  = new Application_Model_Page();
            $model  = $mapper->findByKey($key, $model);

            if (!$model->getEnabled())
            {
                $model  = $mapper->findByKey('404', $model);
            }

            if ($model->getEnabled())
            {
                $this->view->assign('model', $model);
                if ($model->getTitle())
                {
                    $this->view->headTitle($model->getTitle());
                }
                if ($model->getMetaDescription())
                {
                    $this->view->headMeta()->appendName('description', $model->getMetaDescription());
                }
                if ($model->getMetaKeywords())
                {
                    $this->view->headMeta()->appendName('keywords', $model->getMetaKeywords());
                }
            }
        }
    }
}

